package vip.guzb.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.task.TaskExecutor;

/**
 * @author 顾志兵
 * @mail ipiger@163.com
 * @since 2024-03-26
 */
@SpringBootApplication
public class GracefullyShutdownAppMain {

    @Autowired
    private TaskExecutor taskExecutor;

    public static void main(String[] args) {
        SpringApplication.run(GracefullyShutdownAppMain.class, args);
    }

    @EventListener(ApplicationReadyEvent.class)
    public void execute() throws Exception{
        System.out.println("taskExecutor = " + taskExecutor);
        for (int i = 0; i < 3; i++) {
            final int count = i + 1;
            taskExecutor.execute(() -> {
                try {
                    System.out.println("[任务-" + count + "]: 我开始睡觉了哈...");
                    Thread.sleep(20_000);
                    System.out.println("[任务-" + count + "]: 我已经睡醒了喔 ^_^");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }
}
